package com.hethongviet.vending.pages;

import com.hethongviet.vending.AppConfig;
import com.hethongviet.vending.R;
import com.hethongviet.vending.helper.GeneralActivity;
import com.hethongviet.vending.model.Transaction;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.os.Bundle;
import android.content.Intent;
import android.graphics.Bitmap;

public class InputDeviceNumberActivity extends GeneralActivity
{

	private EditText mTextInput = null;
	private TextView mTextErrorNumber = null;
	private Button mButtonGetQrCode = null;
	@SuppressWarnings("unused")
	private ImageView mImageQrCode = null;
	private Button mButtonOk = null;
	@SuppressWarnings("unused")
	private Bitmap mBitmapQRCode = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.page_input_device_number);

		mTextInput = (EditText) findViewById(R.id.text_dialog_input);
		mTextErrorNumber = (TextView) findViewById(R.id.text_dialog_error_number);
		mButtonGetQrCode = (Button) findViewById(R.id.button_get_qr_code);
		mImageQrCode = (ImageView) findViewById(R.id.image_qr_code);
		mButtonOk = (Button) findViewById(R.id.button_dialog_ok);

		mTextInput.addTextChangedListener(new TextWatcher()
		{

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3)
			{
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3)
			{
				mTextErrorNumber.setVisibility(View.INVISIBLE);
			}

			@Override
			public void afterTextChanged(Editable arg0)
			{

				if (arg0.length() > 0)
				{
					if (arg0.length() > 15)
					{
						mButtonOk.setEnabled(false);
						mTextErrorNumber
								.setText(getString(R.string.input_error_number_large));
					} else
						mButtonOk.setEnabled(true);

				} else
				{
					mButtonOk.setEnabled(false);
				}

				try
				{
					Integer.parseInt(arg0.toString());
				} catch (NumberFormatException e)
				{
					e.printStackTrace();
					mButtonOk.setEnabled(false);
					mTextErrorNumber.setVisibility(View.VISIBLE);
				}

			}
		});

		mButtonOk.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{

				// if (mBitmapQRCode == null)
				// {
				// Utils.debug("Null image qr code!!!!!!!!!!");
				// return;
				// }

				if (AppConfig.IS_BLUETOOTH_ONE_SCREEN)
				{
					startActivity(new Intent(InputDeviceNumberActivity.this,
							StandbyActivityNonBluetooth.class));
				} else
				{
					startActivity(new Intent(InputDeviceNumberActivity.this,
							StandbyActivity.class));
				}

			}
		});

		mButtonGetQrCode.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				// start activity get qr-code
			}
		});

	}

	@Override
	public void onConnectFail(int typeError)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetworkMessageIncomming(Transaction transaction)
	{
		// TODO Auto-generated method stub

	}
}
